package com.xjtu.conan.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.conan.entity.ArticleCommentsInfo;
import com.xjtu.conan.entity.ArticleCommentsInfo;
import com.xjtu.conan.exception.MyException;
import com.xjtu.conan.mapper.ArticleCommentsInfoMapper;
import com.xjtu.conan.mapper.ArticleCommentsInfoMapper;
import com.xjtu.conan.service.ArticleCommentsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 话题评论表 服务实现类
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@Service
public class ArticleCommentsInfoServiceImpl extends ServiceImpl<ArticleCommentsInfoMapper, ArticleCommentsInfo> implements ArticleCommentsInfoService {
    @Autowired
    private ArticleCommentsInfoMapper commentsInfoMapper;

    @Override
    public List<ArticleCommentsInfo> getAllArticleCommentsInfo(int current, int size) {
        Page<ArticleCommentsInfo> page = new Page<>(current, size);
        commentsInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public ArticleCommentsInfo getArticleCommentByid(Long acid) {
        ArticleCommentsInfo ArticleComments = commentsInfoMapper.selectById(acid);
        if (ArticleComments == null) {
            throw new MyException(500, "不存在该话题评论！");
        }
        return ArticleComments;
    }

    @Override
    public int addArticleComments(ArticleCommentsInfo articlecommentsInfo) throws IOException {
        try {
            return commentsInfoMapper.insert(articlecommentsInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "话题评论插入失败！");
        }
    }

    @Override
    public List<ArticleCommentsInfo> getArticleCommentsByaid(Long aid) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("article_id", aid);
            return commentsInfoMapper.selectByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "该话题评论数为0！");
        }
    }

    @Override
    public List<ArticleCommentsInfo> getArticleCommentsBypid(Long pid) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("parent_id", pid);
            return commentsInfoMapper.selectByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "该评论的子评论数为0！");
        }
    }

    @Override
    public int deleteArticleComments(Long ncid) {
        try {
            return commentsInfoMapper.deleteById(ncid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "话题评论删除失败！");
        }

    }

    @Override
    public int updateArticleComments(ArticleCommentsInfo ArticlecommentsInfo) {
        try {
            return commentsInfoMapper.updateById(ArticlecommentsInfo);
        } catch (Exception e) {
            throw new MyException(500, "评论更新失败！");
        }
    }


    @Override
    public int getTotal() {
        return commentsInfoMapper.selectCount(null);
    }
}
